package app.view.component 
{
	import flash.desktop.NativeApplication;
	import flash.display.Graphics;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.geom.Rectangle;
	import flash.media.StageWebView;
	import flash.system.Capabilities;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	import com.facebook.graph.*;

	/**
	 * ...
	 * @author PHANCUONG87
	 */
	public class FaceCore 
	{
		protected const APP_ID:String = "191759940841700";// "1407023606227444";//"191759940841700"; // Your App ID.
		protected const APP_ORIGIN:String = "YOUR_APP_ORGIN"; 
		protected var permissions:Array = ["publish_stream","user_website","user_status","user_about_me"];
		
		private static var webView:StageWebView;
		private static var accessToken:String = "";

		private static var user:Object;
		private static var FB_LOGGED_IN:Boolean = false;
		private var isApp:Boolean = false;
		public var stage:Stage;
		
		public function FaceCore() 
		{
			
		}
		
		public function initFacebook():void {
			if (Capabilities.playerType == "Desktop") { 
				isApp = true;
			}
			
			if (isApp) {
				initFbMobile();
			} else {
				initFbWeb();
			}
		}
				 
		private function initFbMobile():void {
			FacebookMobile.init(APP_ID, onFbInit);
		}
				 
		private function initFbWeb():void {
			Facebook.init(APP_ID, onFbInit);
		}
		
		private function onFbInit(success:Object, fail:Object):void {
			if (isApp) {
				connectFbApp();
			} else {
				connectFbWeb();
			}
		}
		
		private function connectFbApp():void {
			if (webView) {
				try {
					webView.dispose();
				}catch (err:Error){}
				
			}
			
			webView = new StageWebView();
			webView.viewPort = new Rectangle(30, 30, stage.stageWidth - 60, stage.stageHeight-60);
			FacebookMobile.login(onFbConnect, stage, ["publish_stream"], webView);
		}
		
		private function connectFbWeb():void {
			Facebook.login(onFbConnect, {scope:"publish_stream"});
		}

		private var userInfo:Object = {};
		 
		private function onFbConnect(success:Object, fail:Object):void {
			if (success) {
				
				userInfo = success;
				accessToken = success.accessToken;
				var postMessage:String = "test cái nào";
				if (isApp) {
					callFbApp("/me/feed", onPosted, {message:postMessage, picture:"http://friendsonfire.brownbearstudios.com/images/icon.png", link:"", name:"DuckGuy", caption:"abc"}, "POST");
				} else {
					callFbWeb("/me/feed", onPosted, {message:postMessage, picture:"http://friendsonfire.brownbearstudios.com/images/icon.png", link:"", name:"DuckGuy", caption:"abc"}, "POST");
				}
			} else {
				if (webView) {
					
					
					try {
						webView.dispose();
					}catch (err:Error){}
					
				}
			}
		}
		
		public function onPosted(e1:*,e2:*):void 
		{
			if (webView) {
				try {
					webView.dispose();
				}catch (err:Error){}
				
			}
		}
		private function callFbApp(method:String, callback:Function, params:*, requestMethod:String):void {
			FacebookMobile.api(method, callback, params, requestMethod);
		}
		 
		private function callFbWeb(method:String, callback:Function, params:*, requestMethod:String):void {
			Facebook.api(method, callback, params, requestMethod);
		}
		
		private function deactivate(e:Event):void 
		{
			// make sure the app behaves well (or exits) when in background
			//NativeApplication.nativeApplication.exit();
		}
	}

}